package com.ncp.model.biz.product;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

import com.ncp.model.dao.DB;
import com.ncp.model.entity.product.ImgsUploadBean;
import com.ncp.model.entity.product.ProductAllInfoBean;
import com.ncp.model.entity.product.ProductBasicBean;
import com.ncp.model.entity.product.ProductBasicLoadBean;
import com.ncp.model.entity.product.ProductDetailBean;
import com.ncp.model.entity.product.ProductSortBean;

import com.ncp.model.dao.DbConnection;


public class ManageFrontProductBiz {
	
	
	/**
	 * @author hhy
	 * 
	 * 
	 */
	
	private DB db=new DB();
	
	/*
	 * 得到所有产品的ID号和产品名称
	 * return 所有产品的ID号和产品名称
	 * */
	public List<ImgsUploadBean> getProductImgInfo(){
		String sql = "select ProductID,ProductName from PRODUCTBASIC ";
		db.setSql(sql);
		
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ImgsUploadBean> list1 = new ArrayList<ImgsUploadBean>();
		for(Map<String,Object> object: rs.getRows()){
			ImgsUploadBean iub = new ImgsUploadBean();
			 iub.setProductID(Integer.parseInt(object.get("ProductID").toString()));
			 iub.setProductName(object.get("ProductName").toString());
			 list1.add(iub);
		
		}
		
		return list1;
	}
	
	/*得到产品种类的名称
	 * @param ProductSortID表示种类的ID号
	 * return 返回产品种类的名称
	 * */
	public String getProductSortName(int ProductSortID){
		String sql = "select ProductSortName from PRODUCTSORT where ProductSortID="+ProductSortID;
		db.setSql(sql);
		
		String ProductSortName=db.getProductItem();
		
		return ProductSortName;
	}
	/*得到产品种类的ID
	 * @param ProductSortName表示种类名称
	 * return 返回产品种类的ID
	 * */
	public int getProductSortID(String ProductSortName){
		
		String sql = "select ProductSortID from PRODUCTSORT where ProductSortName='"+ProductSortName+"'";
		
		db.setSql(sql);
		
		int ProductSortID=Integer.parseInt(db.getProductItem());
		
		return ProductSortID;
	}
	
	/*得到指定ID号的产品信息
	 * @param ProductID表示产品ID号
	 * return 返回所有产品信息
	 * */
	public List<ProductAllInfoBean> getProductAllInfoBean(int ProductID){
		
		
		String sql="select * from PRODUCTBASIC where ProductID="+ProductID;
		db.setSql(sql);
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductAllInfoBean> list1 = new ArrayList<ProductAllInfoBean>();
		for(Map<String,Object> object: rs.getRows()){
			ProductAllInfoBean pbb = new ProductAllInfoBean();
			
			    pbb.setProductID(Integer.parseInt(object.get("ProductID").toString()));
				pbb.setProductName(object.get("ProductName").toString());
				pbb.setProductSortID(Integer.parseInt(object.get("ProductSortID").toString()));
				pbb.setProductImage(object.get("ProductImage").toString());
				pbb.setProductDescription(object.get("ProductDescription").toString());
				pbb.setRetailPrice(Float.parseFloat(object.get("RetailPrice").toString()));
				pbb.setMarketPrice(Float.parseFloat(object.get("MarketPrice").toString()));
				pbb.setProductType(object.get("ProductType").toString());
				pbb.setProductDiscount(Float.parseFloat(object.get("ProductDiscount").toString()));
		        pbb.setCurrentPNumber(Integer.parseInt(object.get("CurrentPNumber").toString()));
				pbb.setSoldPNumber(Integer.parseInt(object.get("SoldPNumber").toString()));
				pbb.setBeginTime(object.get("BeginTime").toString());
				pbb.setEndTime(object.get("EndTime").toString());

			list1.add(pbb);
			
		}

		return list1;
		
	}

	/*得到相关的产品种类信息
	 * @param sql表示需要输入的ＳＱＬ语句
	 * return 返回产品种类信息
	 * */
	public List<ProductSortBean> getProductSort(String sql){
		db.setSql(sql);
		
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductSortBean> list1 = new ArrayList<ProductSortBean>();
		for(Map<String,Object> object: rs.getRows()){
			ProductSortBean psb = new ProductSortBean();
			psb.setProductSortID(Integer.parseInt(object.get("ProductSortID").toString()));
			psb.setProductSortName(object.get("ProductSortName").toString());
            psb.setRootProductSortID(Integer.parseInt(object.get("ParentProductSortID").toString()));
	        psb.setRootProductSortID(Integer.parseInt(object.get("RootProductSortID").toString()));
            psb.setRootProductSortID(Integer.parseInt(object.get("isover").toString()));
	           
			list1.add(psb);
		
		}
		
		return list1;
	}
    
	/*得到所有已下架产品信息
	 * @param sql表示需要输入的ＳＱＬ语句
	 * return 返回所有已下架产品信息
	 * */
	public List<ProductBasicLoadBean> getProductBasicBack(String sql){
		
		db.setSql(sql);
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductBasicLoadBean> list1 = new ArrayList<ProductBasicLoadBean>();
		for(Map<String,Object> object: rs.getRows()){
			ProductBasicLoadBean pbb = new ProductBasicLoadBean();
			
			    pbb.setProductID(Integer.parseInt(object.get("ProductID").toString()));
			    pbb.setProductImage(object.get("ProductImage").toString());
				pbb.setProductName(object.get("ProductName").toString());
				pbb.setEndTime(object.get("EndTime").toString());
				pbb.setBeginTime(object.get("BeginTime").toString());
				pbb.setProductImage(object.get("ProductImage").toString());
				pbb.setRetailPrice(Float.parseFloat(object.get("RetailPrice").toString()));
                pbb.setRetailPrice(Float.parseFloat(object.get("MarketPrice").toString()));
                pbb.setProductType((object.get("ProductType").toString()));
				pbb.setProductDiscount(Float.parseFloat(object.get("ProductDiscount").toString()));
			    pbb.setCurrentPNumber(Integer.parseInt(object.get("CurrentPNumber").toString()));
			    pbb.setSoldPNumber(Integer.parseInt(object.get("SoldPNumber").toString()));

			list1.add(pbb);
			
		}
		
		return list1;
	}
	/*得到满足条件的产品信息
	 *@param s_search表示搜索的内容，或者按照种类的相同显示在同一页面
	 *@param s_order表示按照的顺序
	 *@param　PageNow表示现在显示的页面数
	 *@param　PageSize表示所有一页可以显示多少个产品
	 * return 返回满足条件的产品
	 * */
	public List<ProductBasicBean> getProductBasicItem(String s_search,String s_order,int PageNow,int PageSize){
		 
		String sql = "select ProductID,ProductName,ProductImage,MarketPrice,RetailPrice," +
			"SoldPNumber,ProductDiscount from PRODUCTBASIC "+s_search+" "+s_order+" limit "+(PageNow*PageSize-PageSize)+","+PageSize+"";
			
		db.setSql(sql);
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductBasicBean> list1 = new ArrayList<ProductBasicBean>();
		for(Map<String,Object> object: rs.getRows()){
			   
			ProductBasicBean pbb = new ProductBasicBean();
			    pbb.setProductName(object.get("ProductName").toString());
				pbb.setProductImage(object.get("ProductImage").toString());
				pbb.setRetailPrice(Float.parseFloat(object.get("RetailPrice").toString()));
                pbb.setMarketPrice(Float.parseFloat(object.get("MarketPrice").toString()));
				pbb.setProductDiscount((Float.parseFloat(object.get("ProductDiscount").toString())));
			    pbb.setSoldPNumber(Integer.parseInt(object.get("SoldPNumber").toString()));
			    pbb.setProductID(Integer.parseInt(object.get("ProductID").toString()));
			  

			list1.add(pbb);
			
		}

		return list1;
		
	}

	/*得到产品种类（没有属于它的种类了）的情况
	 * return 返回满足条件所有种类
	 * */
	public List<ProductSortBean> getProductSort(){
		String sql = " select * from PRODUCTSORT where isover=0";
		db.setSql(sql);
	
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductSortBean> list1 = new ArrayList<ProductSortBean>();
		for(Map<String,Object> object: rs.getRows()){
			ProductSortBean psb = new ProductSortBean();
			psb.setProductSortID(Integer.parseInt(object.get("PRODUCTSORTID").toString()));
			psb.setProductSortName(object.get("PRODUCTSORTNAME").toString());
			psb.setParentProductSortID(Integer.parseInt(object.get("PARENTPRODUCTSORTID").toString()));
			list1.add(psb);
		
		}
		
		return list1;
	}
	
	/*得到产品的详细信息
	 *@param ProductID表示需要知道详细信息的产品ID号
	 * return 返回满足条件的产品的详细信息
	 * */
	public List<ProductDetailBean> getProductDetail(int ProductID){
		 String sql = "select ProductName,ProductImage,ProductDescription," +
         "ProductID,RetailPrice,MarketPrice,ProductType,ProductDiscount,"+
         "CurrentPNumber,SoldPNumber FROM PRODUCTBASIC where ProductID="+ProductID;
		db.setSql(sql);
		db.setConn(DbConnection.getConnByDataSource());
		List<Object> list=new ArrayList<Object>();
		db.setList(list);
		Result rs = db.executeQuery();
		List<ProductDetailBean> list1 = new ArrayList<ProductDetailBean>();
		for(Map<String,Object> object: rs.getRows()){
			ProductDetailBean pdb = new ProductDetailBean();
			    pdb.setProductName(object.get("ProductName").toString());
				pdb.setProductImage(object.get("ProductImage").toString());
				pdb.setProductDescription(object.get("ProductDescription").toString());
				pdb.setProductID(Integer.parseInt(object.get("ProductID").toString()));
				pdb.setRetailPrice(Float.parseFloat(object.get("RetailPrice").toString()));
			    pdb.setMarketPrice(Float.parseFloat(object.get("MarketPrice").toString()));
			    pdb.setProductType(object.get("ProductType").toString());
			    pdb.setProductDiscount(Float.parseFloat(object.get("ProductDiscount").toString()));
			    pdb.setCurrentPNumber(Integer.parseInt(object.get("CurrentPNumber").toString()));
			    pdb.setSoldPNumber(Integer.parseInt(object.get("SoldPNumber").toString()));
			    list1.add(pdb);
		
		}
		return list1;
	  }
	
	
	
	
	
}

